<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");

class Order_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function Add_Order($name, $addr, $pay, $b, $curdate, $phone, $id) {
        $order = array(
            'PayID' => $pay,
            'CID' => $id,
            'OStatus' => 1,
            'ODate' => $curdate,
            'OSDate' => $b,
            'OName' => $name,
            'OAddr' => $addr,
            'OPhone' => $phone
        );
        if ($this->db->insert('orders', $order)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function GetOID($cid) {
        $this->db->select('OID');
        $this->db->from('orders');
        $this->db->where('CID', $cid);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return FALSE;
        }
    }

    public function Add_order_detail($oid, $fid, $qty, $price, $amount) {
        $order_detail = array(
            'OID' => $oid,
            'FID' => $fid,
            'OrderQty' => $qty,
            'ODPrice' => $price,
            'Amount' => $amount,
        );
        if ($this->db->insert('orderdetail', $order_detail)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //lay tong hoa don
    public function SumHD($curdate = null, $ostatus = null) {
        $this->db->select("OID");
        if ($curdate != null) {
            $this->db->where("ODate", $curdate);
        }
        if ($ostatus != null) {
            $this->db->where("OStatus", $ostatus);
        }
        $result = $this->db->get("orders");
        if ($result->num_rows() > 0) {
            return $result->num_rows();
        } else {
            return $result = 0;
        }
    }

    //lay tat ca hoa don
    public function GetOrder($query_array = null, $limit, $offset) {

        $q = $this->db->select("orders.OID,PayID,CID,OStatus,ODate,OSDate,OName,OAddr,FID,OrderQty,ODPrice,Amount")
                ->from("orders")
                ->join("orderdetail", "orderdetail.OID = orders.OID")
                ->limit($limit, $offset);
        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->like("OName", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $ret["rows"] = $q->get()->result();


        $q = $this->db->select('count(*) as count', FALSE)->from("orders")->join("orderdetail", "orderdetail.OID = orders.OID");

        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->where("CID", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }

    //lay hoa don trong ngay
    public function  HdTrongNgay($query_array = null, $limit, $offset,$curdate){
        $q = $this->db->select("orders.OID,PayID,CID,OStatus,ODate,OSDate,OName,OAddr,FID,OrderQty,ODPrice,Amount")
                ->from("orders")
                ->join("orderdetail", "orderdetail.OID = orders.OID")
                ->limit($limit, $offset)
                ->where("ODate",$curdate);
        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->like("OName", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $ret["rows"] = $q->get()->result();


        $q = $this->db->select('count(*) as count', FALSE)->from("orders")->join("orderdetail", "orderdetail.OID = orders.OID")->where("ODate",$curdate);

        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->where("CID", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }

    //hoa don chua xu ly
    public function HdChuaXuLy($query_array = null, $limit, $offset){
        $q = $this->db->select("orders.OID,PayID,CID,OStatus,ODate,OSDate,OName,OAddr,FID,OrderQty,ODPrice,Amount")
                ->from("orders")
                ->join("orderdetail", "orderdetail.OID = orders.OID")
                ->limit($limit, $offset)
                ->where("OStatus",1);
        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->like("OName", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $ret["rows"] = $q->get()->result();


        $q = $this->db->select('count(*) as count', FALSE)->from("orders")->join("orderdetail", "orderdetail.OID = orders.OID")->where("OStatus",1);

        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->where("CID", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }
    
    //hoa don da xu ly
    public function HdDaXuLy($query_array = null, $limit, $offset){
        $q = $this->db->select("orders.OID,PayID,CID,OStatus,ODate,OSDate,OName,OAddr,FID,OrderQty,ODPrice,Amount")
                ->from("orders")
                ->join("orderdetail", "orderdetail.OID = orders.OID")
                ->limit($limit, $offset)
                ->where("OStatus",3);
        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->like("OName", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $ret["rows"] = $q->get()->result();


        $q = $this->db->select('count(*) as count', FALSE)->from("orders")->join("orderdetail", "orderdetail.OID = orders.OID")->where("OStatus",3);

        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->where("CID", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }
    //hoa don dang xu ly
    public function HdDangXuLy($query_array = null, $limit, $offset){
        $q = $this->db->select("orders.OID,PayID,CID,OStatus,ODate,OSDate,OName,OAddr,FID,OrderQty,ODPrice,Amount")
                ->from("orders")
                ->join("orderdetail", "orderdetail.OID = orders.OID")
                ->limit($limit, $offset)
                ->where("OStatus",2);
        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->like("OName", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $ret["rows"] = $q->get()->result();


        $q = $this->db->select('count(*) as count', FALSE)->from("orders")->join("orderdetail", "orderdetail.OID = orders.OID")->where("OStatus",2);

        if (strlen($query_array["MHD"]) && $query_array != null) {
            $q->where("orders.OID", $query_array["MHD"]);
        }
        if (strlen($query_array["KH"]) && $query_array != null) {
            $q->where("CID", $query_array["KH"]);
        }
        if (strlen($query_array["ODate"]) && $query_array != null) {
            $query_array["ODate"] = date("Y-m-d", strtotime($query_array["ODate"]));
            $q->where("ODate", $query_array["ODate"]);
        }
        if (strlen($query_array["OSDate"]) && $query_array != null) {
            $query_array["OSDate"] = date("Y-m-d", strtotime($query_array["OSDate"]));
            $q->where("OSDate", $query_array["OSDate"]);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }
    
    //lay hoa don
    public function GetOrderToID($oid) {
        $this->db->where("OID", $oid);
        $query = $this->db->get("orders");
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

    public function GetOrderDetail($oid) {
        $this->db->select("OID,orderdetail.FID,OrderQty,ODPrice,Amount,FName");
        $this->db->from("flowers");
        $this->db->join("orderdetail", "orderdetail.FID = flowers.FID");
        $this->db->where("OID", $oid);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //xoa hoa don
    public function DeleteOrder($oid) {
        $sql = "select * from orders where OID=$oid";
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $row = $query->row();
            if ($row->OStatus != 3) {
                return 1;
            } else {
                mysql_query("delete from orderdetail where OID=$oid");
                mysql_query("delete from orders where OID=$oid");
                return 0;
            }
        }
    }
    
    
    //sua trang thai hoa don
    public function UpdateStt($oid,$stt){
        if($oid != "" && $stt != ""){
            $data = array (
              'OStatus'=> $stt  
            );
            $this->db->where("OID",$oid);
            $sql = $this->db->update("orders",$data);
            if($sql){
                return TRUE;
            }else return FALSE;
        }
    }
}

?>
